###############################
## devising a universal test ##
###############################

library(foreign)
library(effects)
library(nnet)
library(MASS)
library(dplyr)
library(tidyverse)
library(plyr) 
library(haven)
library(simcf)
library(ggplot2)
options(scipen=20)


## read analysis_output
tests <- read_delim(file = "tests_output_lat&ind.txt", delim = "\t", quote = "", col_names = TRUE)

## adjust analysis output so that county1 always has a greater value
## than county2.  This means that county1 is a treatment county.  Note
## that the chi-squared test statistic does not have sides and hence
## flipping does not affect it.
rows2flip <- which(tests$value1 < tests$value2)
for (row2flip in rows2flip) {
  ## flip county names
  temp <- tests$county1[row2flip]     
  tests$county1[row2flip] <- tests$county2[row2flip]    
  tests$county2[row2flip] <- temp
  ## flip values
  temp <- tests$value1[row2flip]     
  tests$value1[row2flip] <- tests$value2[row2flip]    
  tests$value2[row2flip] <- temp
  ## flip proportions
  temp <- tests$proportion1[row2flip]     
  tests$proportion1[row2flip] <- tests$proportion2[row2flip]    
  tests$proportion2[row2flip] <- temp
}

## add variables to test matrix; most important is hours_change_diff,
## which is the difference in some early voting measure.

## set borderankcutoff
borderrankcutoff <- 40

tests <- tests %>% mutate(prop_diff = (proportion1 - proportion2) * 100,
                          "EIP Difference" = (value1 - value2), # names w/ spaces are needed for neat legend titles
                          "Significance" = ifelse(p_value <= 0.05,"p < 0.05","p > 0.05"),
                          race = revalue(race, c("white" = "White", "black" = "Black","latino"="Latino")),
                          party = revalue(party, c("dem" = "Democrat", "rep" = "Republican","ind"="Independent")),
                          group = paste(race, party, sep = ' '))



# filter to look at tests for who voted 

tests<- filter(tests, test_type=="change_distr_vote2016_given_vote2012",
               race != "Latino", party!="Independent")

tests <- tests %>% mutate("pos_neg_tests" = ifelse(prop_diff< 0, "Negative",
                                                   ifelse(prop_diff>0,"Positive", NA)))



# by border measure
x<-prop.table(table(tests$pos_neg_tests, tests$border_measure),2)

xt <- xtable(x = x,
             align = "lccccc",
             type="latex",
             digits = 3,
             caption = "Porportion of Tests of 2016 Voter Turnout that were Positive and Negative, by Type of EIP Change", 
             label = "tab:universal_border_measure")

addtorow <- list()
addtorow$pos <- list(nrow(xt))
addtorow$command <- c(paste0("\\hline\n\\multicolumn{6}{l}{\\emph{Note: Proportions include both significant and insignificant tests.}}\\\\\n"))

print (xt, 
       size="\\footnotesize",
       format.args = list(big.mark = ","),
       caption.placement = "top",
       file = "universal_by_bordermeasure.tex", 
       #include.rownames = FALSE,
       hline.after = c(-1, 0),
       add.to.row = addtorow)


# total hours, by race/party subgroup
total_df<- filter(tests, c(border_measure == "totalhoursDID"))

x<-prop.table(table(total_df$pos_neg_tests,total_df$group),2)

xt <- xtable(x = x,
             align = "lcccc",
             type="latex",
             digits = 3,
             caption = "Porportion of Tests of Changes to Total Hours and 2016 Turnout that were Positive and Negative, by Race/Party Subgroup", 
             label = "tab:universal_totalhours")

addtorow <- list()
addtorow$pos <- list(nrow(xt))
addtorow$command <- c(paste0("\\hline\n\\multicolumn{5}{l}{\\emph{Note: Proportions include both significant and insignificant tests.}}\\\\\n"))

print (xt, 
       size="\\footnotesize",
       format.args = list(big.mark = ","),
       caption.placement = "top",
       file = "universal_total_by_raceparty.tex", 
       #include.rownames = FALSE,
       hline.after = c(-1, 0),
       add.to.row = addtorow)


# evening hours, by race/party subgroup
total_df<- filter(tests, c(border_measure == "evehoursDID"))
x<-prop.table(table(total_df$pos_neg_tests,total_df$group),2)

xt <- xtable(x = x,
             align = "lcccc",
             type="latex",
             digits = 3,
             caption = "Porportion of Tests of Changes to Evening Hours and 2016 Turnout that were Positive and Negative, by Race/Party Subgroup", 
             label = "tab:universal_evehours")

addtorow <- list()
addtorow$pos <- list(nrow(xt))
addtorow$command <- c(paste0("\\hline\n\\multicolumn{5}{l}{\\emph{Note: Proportions include both significant and insignificant tests.}}\\\\\n"))

print (xt, 
       size="\\footnotesize",
       format.args = list(big.mark = ","),
       caption.placement = "top",
       file = "universal_eve_by_raceparty.tex", 
       #include.rownames = FALSE,
       hline.after = c(-1, 0),
       add.to.row = addtorow)

# saturday hours, race and party
total_df<- filter(tests, c(border_measure == "sathoursDID"))
x<-prop.table(table(total_df$pos_neg_tests,total_df$group),2)

xt <- xtable(x = x,
             align = "lcccc",
             type="latex",
             digits = 3,
             caption = "Porportion of Tests of Changes to Saturday Hours and 2016 Turnout that were Positive and Negative, by Race/Party Subgroup", 
             label = "tab:universal_sathours")

addtorow <- list()
addtorow$pos <- list(nrow(xt))
addtorow$command <- c(paste0("\\hline\n\\multicolumn{5}{l}{\\emph{Note: Proportions include both significant and insignificant tests.}}\\\\\n"))

print (xt, 
       size="\\footnotesize",
       format.args = list(big.mark = ","),
       caption.placement = "top",
       file = "universal_sat_by_raceparty.tex", 
       #include.rownames = FALSE,
       hline.after = c(-1, 0),
       add.to.row = addtorow)


# sunday hours, race and party
total_df<- filter(tests, c(border_measure == "sunhoursDID"))
x<-prop.table(table(total_df$pos_neg_tests,total_df$group),2)

xt <- xtable(x = x,
             align = "lcccc",
             type="latex",
             digits = 3,
             caption = "Porportion of Tests of Changes to Sunday Hours and 2016 Turnout that were Positive and Negative, by Race/Party Subgroup", 
             label = "tab:universal_sunhours")

addtorow <- list()
addtorow$pos <- list(nrow(xt))
addtorow$command <- c(paste0("\\hline\n\\multicolumn{5}{l}{\\emph{Note: Proportions include both significant and insignificant tests.}}\\\\\n"))

print (xt, 
       size="\\footnotesize",
       format.args = list(big.mark = ","),
       caption.placement = "top",
       file = "universal_sun_by_raceparty.tex", 
       #include.rownames = FALSE,
       hline.after = c(-1, 0),
       add.to.row = addtorow)


# sites, race and party
total_df<- filter(tests, c(border_measure == "sitesDID"))
x<-prop.table(table(total_df$pos_neg_tests,total_df$group),2)

xt <- xtable(x = x,
             align = "lcccc",
             type="latex",
             digits = 3,
             caption = "Porportion of Tests of Changes to EIP Sites and 2016 Turnout that were Positive and Negative, by Race/Party Subgroup", 
             label = "tab:universal_sites")

addtorow <- list()
addtorow$pos <- list(nrow(xt))
addtorow$command <- c(paste0("\\hline\n\\multicolumn{5}{l}{\\emph{Note: Proportions include both significant and insignificant tests.}}\\\\\n"))

print (xt, 
       size="\\footnotesize",
       format.args = list(big.mark = ","),
       caption.placement = "top",
       file = "universal_sites_by_raceparty.tex", 
       #include.rownames = FALSE,
       hline.after = c(-1, 0),
       add.to.row = addtorow)


stop()
